package com.oocl.o2o.client.service.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Repository;

import com.oocl.o2o.client.dao.FoodDao;
import com.oocl.o2o.client.dao.MerchantDao;
import com.oocl.o2o.client.entity.Food;
import com.oocl.o2o.client.entity.Merchant;
import com.oocl.o2o.client.service.FoodService;
import com.oocl.o2o.client.util.ConfigBaseUtil;

/**
 * @description: 
 * @author  XIEMI2
 * @date Aug 12, 2017 8:00:19 PM
 * @version 1.0
 */
@Repository(value="foodService")
public class FoodServiceImpl implements FoodService{
	
	@Resource
	private FoodDao foodDao;
	@Resource
	private MerchantDao merchantDao;
	
	@Transactional
	@Override
	public Food add(Food food) {
		String imgString = food.getImage();
		food.setImage(ConfigBaseUtil.getPropertiesInfo("imgPrefix")+imgString);
		return foodDao.add(food);
	}

	@Transactional
	@Override
	public Food delete(String id) {
		return foodDao.delete(id);
	}

	@Transactional
	@Override
	public Food update(Food food) {
		return foodDao.update(food);
	}

	@Override
	public Food findById(String id) {
		Food food = foodDao.findById(id);
		String imgString = food.getImage();
		food.setImage(ConfigBaseUtil.getPropertiesInfo("imgPrefix")+imgString);
		return food;
	}

	@Override
	public List<Food> findByMerchantId(String merchantId) {
		return foodDao.findByMerchantId(merchantId);
	}

	@Override
	public List<Food> findByMerchantName(String merchantName) {
		Merchant merchant = merchantDao.findByUsername(merchantName);
		 List<Food> foods = merchant.getFoods();
		 for(Food f:foods){
			 f.setImage(ConfigBaseUtil.getPropertiesInfo("imgPrefix")+f.getImage());
		 }
		return foods;
	}

	@Override
	public List<Food> findByTypeId(String typeId) {
		 List<Food> foods = foodDao.findByTypeId(typeId);
		 for(Food f:foods){
			 f.setImage(ConfigBaseUtil.getPropertiesInfo("imgPrefix")+f.getImage());
		 }
		return foods;		
	}

	@Override
	public List<Food> findAll() {
		List<Food> foods = foodDao.findAll();
		 for(Food f:foods){
			 f.setImage(ConfigBaseUtil.getPropertiesInfo("imgPrefix")+f.getImage());
		 }
		return foods;	
	}

	@Override
	public List<Food> findByTypeIdAndMerchantId(String typeId, String merchantId) {
		List<Food> foods = foodDao.findByTypeIdAndMerchantId(typeId, merchantId);
		 for(Food f:foods){
			 f.setImage(ConfigBaseUtil.getPropertiesInfo("imgPrefix")+f.getImage());
		 }
		return foods;
	}

	@Override
	public List<Food> findByFoodKeyWord(String keyWord) {
		List<Food> foods = foodDao.findByFoodKeyWord(keyWord);
		 for(Food f:foods){
			 f.setImage(ConfigBaseUtil.getPropertiesInfo("imgPrefix")+f.getImage());
		 }
		return foods;
	}

}


